import $http from '@/api'
import * as types from '../mutation-types'
// initial state
const state = {
  userInfo: null
}

// getters
const getters = {
  getUserInfo: (state) => {
    return state.userInfo
  }
}

// actions
const actions = {
  getRequestData ({ commit }) {
    return $http.getUserInfo()
      .then(res => {
        if (res.code === 0) {
          commit(types.RECEIVE_USER_INFO, res.data)
          return res
        }
      })
      .catch(err => {
        console.log(err)
        return err
      })
  }
}

// mutations
const mutations = {
  [types.RECEIVE_USER_INFO] (state, data) {
    state.userInfo = data
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
